Springcloud |
您所在的位置:网站首页 › spring cloud alibaba服务降级 › Springcloud |
Springcloud-Alibaba 〖九〗Hystrix篇
PS: github仓库[仓库地址](https://github.com/ktoking/springcloud-alibaba)项目都放到里面了一. 分布式系统面临的问题服务雪崩
二. Hystrix是什么?三. 新建模块module cloud-provider-hystrix-payment80013.1 目录结构3.2 改pom3.3 建 application.yml3.4 主启动类3.5 业务类3.6 测试
四. 压力测试五. 新建cloud-consumer-feign-hystrix-order80服务5.1 项目目录5.2 pom5.3 application.yml5.4 主启动5.5 业务类
六. 服务降级(8001项目)6.1 主启动开启Hystrix6.2 添加兜底方法6.3 全局服务降级@DefaultProperties6.4 客户端的服务降级
七. 服务熔断7.1 服务熔断8001服务7.2 controller服务7.3 测试7.4 总结
八. 服务限流九. Hystrix DashBoard 图形化界面搭建module cloud-consumer-hystrix-dashboard90019.1 工程目录9.2 pom9.3 application.yml9.4 主启动9.5 测试9.5.1 PaymentHystrixMain8001 添加代码9.5.2 开启三个服务9.5.3 输入如下点击监控9.5.4 查看监控平台9.5.5 怎么看监控平台
Hystrix篇完,可以把精力放在后面跟他更重要的Sentine上~关注点赞不迷路转载要标注~
PS: github仓库仓库地址项目都放到里面了
一. 分布式系统面临的问题
复杂分布式体系结构中的应用程序有数十个依赖关系,每个依赖关系在某些时候将不可避免地损失的。 服务雪崩多个微服务之间调用的时候,假设微服务A调用微服务B和微服务C,微服务B和微服务C又调用其它的微服务,这就是所谓的"扇出"。 如果扇出的链路上某个微服务的调用响应时间过长或者不可用,对微服务A的调用就会占用越来越多的系统资源,进而引起系统崩溃,所谓的“雪崩效应”. 对于高流量的应用来说,单-的后端依赖可能会导致所有服务器上的所有资源都在几秒钟内饱和。比失败更糟糕的是,这些应用程序还可能导致服务之间的延迟增加,备份队列,线程和其他系统资源紧张,导致整个系统发生更多的级联故障。这些都表示需要对故障和延迟进行隔离和管理,以便单个依赖关系的失败,不能取消整个应用程序或系统。 所以,通常当你发现一个模块下的某个实例失败后,这时候这个模块依然还会接收流量,然后这个有问题的模块还调用了其他的模块,这样就会发生级联故障,或者叫雪崩。 二. Hystrix是什么?Hystrix是一个用于处理分布式系统的延迟和容错的开源库, 在分布式系统里,许多依赖不可避免的会调用失败,比如超时、异常等 Hystrix能够保证在一个依赖出问题的情况下,不会导致整体服务失败,避免级联故障,以提高分布式系统的弹性。 “断路器”本身是一种开关装置,当某个服务单元发生故障之后,通过断路器的故障监控(类似熔断保险丝),向调用方返回一个符合预期的、可处理的备选响应(FallBack) ,而不是长时间的等待或者抛出调用方无法处理的异常,这样就保证了服务调用方的线程不会被长时间、不必要地占用,从而避免了故障在分布式系统中的蔓延,乃至雪崩。 但是停更了…,不影响先学一波,毕竟设计理论与思想最重要嘛,大家都是抄作业,像阿里的sentinel…会不会我到时候也自研一个哈哈~ 三. 新建模块module cloud-provider-hystrix-payment8001 3.1 目录结构service层. package com.atguigu.springcloud.service; import org.springframework.stereotype.Service; import java.util.concurrent.TimeUnit; @Service public class PaymentService { public String paymentInfoOK(Integer id){ return "当前线程: "+Thread.currentThread().getName()+"paymentInfo_OK,id:"+id+"\t"+"O(∩_∩)O哈哈~"; } public String paymentInfoTimeOut(Integer id){ int timeout=3; try { TimeUnit.SECONDS.sleep(timeout); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:"+Thread.currentThread().getName()+" paymentInfo_Timeout,id:"+id+"\t"+"O(∩_∩)O哈哈~"+" 耗时(秒):"+timeout; } }controller层 package com.atguigu.springcloud.controller; import com.atguigu.springcloud.service.PaymentService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class PaymentHystrixController { @Resource private PaymentService paymentService; @Value("${server.port}") private String serverPort; @GetMapping(value = "/payment/hystrix/ok/{id}") public String paymentInfoOK(@PathVariable("id") Integer id){ String result = paymentService.paymentInfoOK(id); log.info("*****result:"+result); return result; } @GetMapping(value = "/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentService.paymentInfoTimeOut(id); log.info("*****result:"+result); return result; } } 3.6 测试正常的方法 安装JMeter 官网安装链接 百度网盘下载 提取码:ibc9 去到Bin目录下,打开如图,即可启动JMeter
service package com.atguigu.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(value = "CLOUD-PROVIDER-HYSTRIX-PAYMENT") public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") String paymentInfoOK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") String paymentInfoTimeOut(@PathVariable("id") Integer id); }controller package com.atguigu.springcloud.controller; import com.atguigu.springcloud.service.PaymentHystrixService; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RestController; import javax.annotation.Resource; @RestController @Slf4j public class OrderHystrixController { @Resource private PaymentHystrixService paymentHystrixService; @GetMapping(value ="/consumer/payment/hystrix/ok/{id}") public String paymentInfoOK(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfoOK(id); return result; } @GetMapping(value ="/consumer/payment/hystrix/timeout/{id}") public String paymentInfo_TimeOut(@PathVariable("id") Integer id){ String result = paymentHystrixService.paymentInfoTimeOut(id); return result; } } 六. 服务降级(8001项目) 6.1 主启动开启HystrixfallbackMethod方法加到可能出错的服务上,再多写一个兜底的方法,只要访问时长超过3s,或服务异常,直接调用我们的兜底方法 @HystrixCommand(fallbackMethod = "paymentInfoTimeOutHandler",commandProperties = { @HystrixProperty(name = "execution.isolation.thread.timeoutInMilliseconds",value = "3000") }) public String paymentInfoTimeOut(Integer id){ int timeout=5; try { TimeUnit.SECONDS.sleep(timeout); } catch (InterruptedException e) { e.printStackTrace(); } return "线程池:"+Thread.currentThread().getName()+" paymentInfo_Timeout,id:"+id+"\t"+"O(∩_∩)O哈哈~"+" 耗时(秒):"+timeout; } public String paymentInfoTimeOutHandler(Integer id){ return "程序运行繁忙或报错,请稍后再试*****"+"当前线程: "+Thread.currentThread().getName()+id+"\t "+"orz!"; } }测试 我们不可能每写一个方法就写一个fallback方法,我们可以同意的处理这些fallback方法 指定全局降级的方法. 调用规则 如果加了之前的详细配置规则,那就还是按照之前的规则来进行,比如设置最大不超过1秒.需要哪个方法去有全局的降级规则,就单一的添加@HystrixCommand注解![]() ![]() 建立Service接口的实现类即可,这样既解耦而且代码量处理的很少,不用在一个controller里面进行大量的代码堆积,解决了代码膨胀与混乱 package com.atguigu.springcloud.service; import org.springframework.cloud.openfeign.FeignClient; import org.springframework.stereotype.Component; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; @Component @FeignClient(value = "cloud-provider-hystrix-payment",fallback = PaymentHystrixServiceImpl.class) public interface PaymentHystrixService { @GetMapping("/payment/hystrix/ok/{id}") String paymentInfoOK(@PathVariable("id") Integer id); @GetMapping("/payment/hystrix/timeout/{id}") String paymentInfoTimeOut(@PathVariable("id") Integer id); }实现其中的方法,这样即可在每个单的的方法中去实现fallback方法 微服务架构大神博客: 博客链接 来到8001service,添加方法 @HystrixCommand(fallbackMethod="paymentCircuitBreakerFallback", commandProperties={ @HystrixProperty(name = "circuitBreaker.enabled" ,value = "true"),//是否开启断路器 @HystrixProperty(name = "circuitBreaker.requestVolumeThreshold",value = "10"),// 请求次数 @HystrixProperty(name = "circuitBreaker.sleepWindowInMilliseconds",value = "10000"),//时间窗口期 " @HystrixProperty(name = "circuitBreaker.errorThresholdPercentage",value = "60")//失败率达到多少后跳闸 }) public String paymentCircuitBreaker(@PathVariable("id") Integer id){ if (id return "id不能为负数,请稍后再试~ id: "+ id; }可以去HystrixCommandProperties类里面去查看配置的注解参数名
设计熔断的三个参数 涉汲到断路器的三个重要参数:快照时间窗、请求总数阀值、错误百分比阀值。 1:快照时间窗:断路器确定是否打开需要统计一些请求和错误数据,而统计的时间范围就是快照时间窗,默认为最近的10秒。2:请求总数阀值:在快照时间窗内,必须满足请求总数阀值才有资格熔断。默认为20, 意味着在10秒内,如果该hystrix命令的调用次数不足20次,即使所有的请求都超时或其他原因失败,断路器都不会打开。3:错误百分比阀值:当请求总数在快照时间窗内超过了阀值,比如发生了30次调用,如果在这30次调用中,有15次发生了超时异常,也就是超过50%的错误百分比,在默认设定50%阀值情况下,这时候就会将断路器打开。原来的主逻辑要如何恢复呢? 对于这一-问题,hystrix也为我们实现了自动恢复功能。 当断路器打开,对主逻辑进行熔断之后,hystrix会启动一个休眠时间窗在这个时间窗内,降级逻辑是临时的成为主逻辑,当休眠时间窗到期,断路器将进入半开状态,释放一次请求到原来的主逻辑上,如果此次请求正常返回,那么断路器将继续闭合,主逻辑恢复,如果这次请求依然有问题,断路器继续进入打开状态,休眠时间窗重新计时。 八. 服务限流这里不多BB,之后带你们去看alibaba的Sentineli,溜了溜了~ 九. Hystrix DashBoard 图形化界面搭建module cloud-consumer-hystrix-dashboard9001但凡是监控,这两个一定要有 打开http://localhost:9001/hystrix就能看到监控平台 我们一直访问8001项目之前正确的服务,在监控平台上就会出现对服务的数据监控
七种颜色对应不同的事件 |
今日新闻 |
推荐新闻 |
CopyRight 2018-2019 办公设备维修网 版权所有 豫ICP备15022753号-3 |